<template>
  <div>
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="trainer.name"/>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="trainer.sort" controls-position="right"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="trainer.level" clearable placeholder="请选择">
          <el-option value="1" label="高级讲师"/>
          <el-option value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="trainer.career"/>
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8080/file/upload"
          :show-file-list="false"
          name="file"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="讲师简介">
        <el-input v-model="trainer.intro" :rows="10" type="textarea"/>
      </el-form-item>


      <!-- saveBtnDisabled  为了防止重复提交
           提交成功设置为 true 不能点击
        -->
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveTrainer">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  import trainer from "@/api/trainer";

  export default {
    name: "add",
    data() {
      return {
        saveBtnDisabled: false,
        trainer: {},
        imageUrl:""
      }
    },
    methods: {

      //图片删除成功后的函数
      handleAvatarSuccess(resp){
        this.imageUrl = resp.data;
        //把图片的Url 复制给存储对象
        this.trainer.avatar = resp.data;
     },


      saveTrainer() {
        trainer.saveTrainer(this.trainer).then(resp=>{

          //提示  防止重复点击设置成 true
          this.saveBtnDisabled = true;
          this.$message({
            message: resp.message,
            type: 'success'
          });
          //页面跳转  动态图 进行跳转
          this.$router.push("/trainer/list")
        });
      }
    }
  }
</script>

<style scoped>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
